﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: GenomeBase.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-12-08 8:26 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;

namespace BOL.Algorithms.Optimization.EvolutionaryAlgorithms
{
    public abstract class GenomeBase<TGene> : IGenome<TGene>
    {
        #region Protected variables

        protected readonly Random Random;

        #endregion

        #region Public properties

        public double Fitness { get; set; }

        #endregion

        #region Constructors

        protected GenomeBase(Random random)
        {
            Random = random;
        }

        #endregion

        #region ICloneable implementation

        protected abstract IGenome<TGene> Clone();

        object ICloneable.Clone()
        {
            return Clone();
        }

        #endregion

        #region IComparable, IComparable<IGenome<TGene>> implementation

        public int CompareTo(object other)
        {
            if (other == null)
                throw new ArgumentNullException("other");

            if (!(other is IGenome<TGene>))
                throw new InvalidCastException("The 'other' argument is not a IGenome<TGene> object.");

            return CompareTo(other as IGenome<TGene>);
        }

        public int CompareTo(IGenome<TGene> other)
        {
            return Fitness.CompareTo(other.Fitness);
        }

        #endregion

        #region Public methods

        public abstract void Evaluate();

        public abstract IGenome<TGene> Offspring();

        public abstract void Crossover(IGenome<TGene> other);

        public abstract void Mutate(double mutationRate);

        #endregion
    }

    public abstract class LamarckianGenomeBase<TGene, TPhenome> : GenomeBase<TGene>, ILamarckianGenome<TGene, TPhenome>
    {
        protected LamarckianGenomeBase(Random random)
            : base(random)
        {
        }

        public abstract void Optimize();
    }
}
